﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Runtime.Configuration;
using PServiceBus.Core.Runtime.Extensions;

namespace PServiceBus.Redis {
    public class KeyValueStore : IKeyValueStore {

        Provider<string> _provider;
        public KeyValueStore() {
            _provider = new Provider<string>() { 
                Endpoint = ConfigurationProvider.ServiceBusConfig.KeyValueStoreAddress,
                IDFunc = o => Guid.NewGuid() };
        }

        #region IKeyValueStore Members

        public ulong Incr(string key) {
            var value = _provider.Incr(key);
            if (value == 0) value = _provider.Incr(key);
            return value;
        }

        public ulong Decr(string key) {
            return _provider.Decr(key);
        }

        public void Set<TValue>(string key, TValue value) {
            _provider.Set<TValue>(key, value);
        }

        public TValue Get<TValue>(string key) {
            return _provider.Get<TValue>(key);
        }

        #endregion
    }
}
